<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/7
 * Time: 14:25
 */

namespace zhtfrontend\controllers;

use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Classroom;
use zhtfrontend\models\datas\ClassroomAttr;
use zhtfrontend\models\datas\Mechanic;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\BadRequestHttpException;

class ClassroomattrController extends BaseController
{

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $classroomattr = ClassroomAttr::find()->orderBy(['classroom_id' => SORT_ASC, 'classday' => SORT_ASC, 'start_time' => SORT_ASC]);
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['classroom_name'])) {
                $class_room_id = Classroom::find()->select(['id'])->where(['like', 'name', $search_data['classroom_name']])->asArray()->column();
                $classroomattr->andWhere(['in', 'classroom_id', $class_room_id]);
            }
            if (!empty($search_data['start_time'])) {
                $start_time = (int)str_replace(':', '', $search_data['start_time']);
                $classroomattr->andWhere(['start_time' => $start_time]);
            }
            if (!empty($search_data['classday'])) {
                $classroomattr->andWhere(['classday' => $search_data['classday']]);
            }
            if (!empty($search_data['mechanic_name'])) {
                $searchmids = Mechanic::find()->select('id')->where(['like', 'name', $search_data['mechanic_name']])->column();
                $searchclassroomids = [];
                if ($searchmids) {
                    $searchclassroomids = Classroom::find()->select('id')->where(['mechanic_id' => $searchmids])->column();
                }
                $classroomattr->andWhere(['classroom_id' => $searchclassroomids]);
            }
        }

        $mids = Fun::getMechanicIds();
        if ($mids) {
            $classroomids = Classroom::find()->select('id')->where(['mechanic_id' => $mids])->column();
            $classroomattr->andWhere(['classroom_id' => $classroomids]);
        }
        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'business_director') {
            $DirectorMechanicIds = Fun::getDirectorMechanicIds();
            $classroomids = Classroom::find()->select('id')->where(['mechanic_id' => $DirectorMechanicIds])->column();
            $classroomattr->andWhere(['classroom_id' => $classroomids]);

        }
        $pages = new Pagination(['totalCount' => $classroomattr->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $classroom_attr = $classroomattr->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($classroom_attr, ['classroom_name', 'classroom_number', 'user_name', 'zhtfrontend_operate']);
        return $this->render('index',['data'=>$array,'pages'=>$pages]);
    }

    /*
     * 产品添加
     */
    public function actionCreate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $model = new ClassroomAttr();
        $classroomModel = Classroom::find()->select(['id', 'name']);
        $mids = Fun::getMechanicIds();
        if ($mids) {
            $classroomModel->where(['mechanic_id' => $mids]);
        }
        $classroom = $classroomModel->asArray()->all();
        $classroom = ArrayHelper::map($classroom, 'id', 'name');
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->start_time = (int)str_replace(':', '', $model->start_time);
            $model->end_time = (int)str_replace(':', '', $model->end_time);
            $model->user_id = \Yii::$app->user->id;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['classroomattr/index']));
            }
        }

        return $this->render('create', ['model' => $model, 'classroom' => $classroom]);
    }

    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model = ClassroomAttr::findOne(\Yii::$app->request->get('id'));
        if ($model === null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $len = strlen($model->start_time) - 2;
        $lenth = strlen($model->end_time) - 2;
        $model->start_time = $len == 1 ? '0' . floor($model->start_time / 100) . ':' . substr($model->start_time, 1) : floor($model->start_time / 100) . ':' . substr($model->start_time, 2);
        $model->end_time = $lenth == 1 ? '0' . floor($model->end_time / 100) . ':' . substr($model->end_time, 1) : floor($model->end_time / 100) . ':' . substr($model->end_time, 2);
        $mids = Fun::getMechanicIds();
        $classroomModel = Classroom::find();
        if ($mids) {
            $classroomModel->where(['mechanic_id' => $mids]);
        }
        $classroom = $classroomModel->asArray()->all();
        $classroom = ArrayHelper::map($classroom, 'id', 'name');
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->start_time = (int)str_replace(':', '', $model->start_time);
            $model->end_time = (int)str_replace(':', '', $model->end_time);
            $model->user_id = \Yii::$app->user->id;
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '修改成功'), Fun::url(['classroomattr/index']));
            }
        }
        return $this->render('create', ['model' => $model, 'classroom' => $classroom]);
    }

    public function actionDelete()
    {
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model = ClassroomAttr::findOne(\Yii::$app->request->get('id'));
        if ($model === null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
//        if($model->status==1){
//            throw new BadRequestHttpException(\Yii::t('zhtfrontend','处于占用状态，不能删除！'));
//        }else{
        if ($model->delete()) {
            return $this->redirectMessage(\Yii::t('zhtfrontend', '删除成功'), Fun::url(['classroomattr/index']));
        }
//        }
    }
}
